package com.example.villagedevelop.generator.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.villagedevelop.generator.entity.House;
import com.example.villagedevelop.generator.entity.HouseVO;
import com.example.villagedevelop.generator.entity.ImageHouse;
import com.example.villagedevelop.generator.mapper.HouseMapper;
import com.example.villagedevelop.generator.mapper.ImageHouseMapper;
import com.example.villagedevelop.generator.mapper.VideoHouseMapper;
import com.example.villagedevelop.generator.service.HouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.villagedevelop.generator.service.ImageHouseService;
import com.example.villagedevelop.generator.service.VideoHouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author villagedevelop
 * @since 2021-11-13
 */
@Service
public class HouseServiceImpl extends ServiceImpl<HouseMapper, House> implements HouseService {
    @Autowired
    HouseMapper mapper;
    @Autowired
    ImageHouseService service;
    @Autowired
    ImageHouseMapper imageHouseMapper;
    @Autowired
    HouseService houseService;
    @Autowired
    VideoHouseService videoHouseService;

    @Override//删除了分页功能，此方法现在相当于findAll，即按默认排序返回所有House数据
    public List<HouseVO> findHouseVOByPageAndOrder() {
//        Page<House> page = new Page<>(current,5);
//        QueryWrapper wrapper = new QueryWrapper();
//
//        IPage<House> result = mapper.selectPage(page,wrapper);
//        List<House> list = result.getRecords();
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("default_sort");
        List<House> list = mapper.selectList(wrapper);

//        for (House ca : list) {
//            HouseVO houseVO = new HouseVO();
//            BeanUtils.copyProperties(ca,houseVO);
//            int hid = ca.getId();
//            houseVO.setImages(service.getImagesByHid(hid));
//            houseVOList.add(houseVO);
//        }
        return this.houseListToVOList(list);

    }

    /**
     *添加新数据
     */
    @Override
    public void insertHouseVO(HouseVO houseVO) {
        House house = new House();
        BeanUtils.copyProperties(houseVO,house);
        mapper.insert(house);
        house.setDefaultSort(house.getId());
        QueryWrapper<House> wrapper = new QueryWrapper<House>();
        wrapper.eq("id",house.getId());
        mapper.update(house,wrapper);
        //插入图片
        ImageHouse image = new ImageHouse();
        int hid = house.getId();
        if (houseVO.getImages() != null){
            for (String url : houseVO.getImages()){
                image.setHId(hid);
                image.setImageFile(url);
                imageHouseMapper.insert(image);
            }
        }
    }

    //通过town查询
    @Override
    public List<HouseVO> findHouseVOBytown(String town) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("town",town);
        List<House> houseList = mapper.selectList(wrapper);

        return houseService.houseListToVOList(houseList);
    }

    //根据town查找其关联的下层village
    @Override
    public List<String> findVillage(String town) {
        List<HouseVO> houseVOList = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("town",town);
        List<House> houseList = mapper.selectList(wrapper);

        List<String> villages = new ArrayList<>();
        //去重
        for (House house:houseList) {
            villages.add(house.getVillage());
        }
        LinkedHashSet<String> hashSet = new LinkedHashSet<>(villages);
        ArrayList<String> villageList = new ArrayList<>(hashSet);
        return villageList;

    }

    //通过town和village查找
    @Override
    public List<HouseVO> findHouseVOBytown_village(String town, String village) {
        QueryWrapper wrapper = new QueryWrapper();
        Map<String,String> map = new HashMap<>();
        map.put("town",town);
        map.put("village",village);
        wrapper.allEq(map);
        List<House> houseList = mapper.selectList(wrapper);

        return houseService.houseListToVOList(houseList);
    }

    //范围筛查查找
    @Override
    public List<HouseVO> findHouseVO_Classify(String field, int min, int max) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.between(field,min,max);
        List<House> houseList = mapper.selectList(wrapper);

        return houseService.houseListToVOList(houseList);
    }


    @Override
    public List<HouseVO> findHouseVO_Classify_order(String field, int method, int min, int max) {
        List<HouseVO> houseVOList = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.between(field,min,max);
        if (method == 1){
            wrapper.orderByAsc(field);
        }else if (method == 2){
            wrapper.orderByDesc(field);
        }
        List<House> houseList = mapper.selectList(wrapper);

        return houseService.houseListToVOList(houseList);
    }

    //按property查找
    @Override
    public List<HouseVO> findHouseVO_Classify_Property(String property) {
        List<House> houseList = new ArrayList<>();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("property",property);
        houseList = mapper.selectList(wrapper);
        return houseService.houseListToVOList(houseList);
    }

    //改变默认排序
    @Override
    public void changeDefaultSort(int id1, int id2) {
        int sid1,sid2;

        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id1);
        House house1 = houseService.getOne(wrapper);
        sid1 = house1.getDefaultSort();
        wrapper = new QueryWrapper<>();
        wrapper.eq("id",id2);
        House house2 = houseService.getOne(wrapper);
        sid2 = house2.getDefaultSort();
        house1.setDefaultSort(sid2);
        house2.setDefaultSort(sid1);
        mapper.updateById(house1);
        mapper.updateById(house2);
    }

    //删除
    @Override
    public void deleteObject(int id) {
        QueryWrapper<House> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        mapper.delete(wrapper);

        QueryWrapper<ImageHouse> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("h_id",id);
        imageHouseMapper.delete(wrapper1);
    }


    //将House的list转换成HouseVO的list
    @Override
    public List<HouseVO> houseListToVOList(List<House> houseList) {
        List<HouseVO> houseVOList = new ArrayList<>();

        for (House ca : houseList) {
            HouseVO houseVO = new HouseVO();
            BeanUtils.copyProperties(ca,houseVO);
            int hid = ca.getId();
            houseVO.setImages(service.getImagesByHid(hid));
            houseVO.setVideoes(videoHouseService.getVideoesByHid(hid));
            houseVOList.add(houseVO);
        }
        return houseVOList;
    }




}
